﻿using System.Collections;
using System.Linq;
namespace YanZhiwei.DotNet3._5.Utilities.Common
{
    /// <summary>
    /// Enumerable帮助类
    /// </summary>
    public static class EnumerableHelper
    {
        /// <summary>
        /// IEnumerable比较
        /// <para>参考：http://www.bluefaq.com/csharp/28920 </para>
        /// </summary>
        /// <param name="first">IEnumerable对象一</param>
        /// <param name="second">IEnumerable对象二</param>
        /// <returns>是否相等</returns>
        public static bool IsEquivalentTo(this IEnumerable first, IEnumerable second)
        {
            var secondList = second.Cast<object>().ToList();
            foreach (var item in first)
            {
                var index = secondList.FindIndex(item.Equals);
                if (index < 0)
                {
                    return false;
                }
                secondList.RemoveAt(index);
            }
            return secondList.Count == 0;
        }
        /// <summary>
        /// IEnumerable包含判断
        /// <para>参考：http://www.bluefaq.com/csharp/28920 </para>
        /// </summary>
        /// <param name="first">IEnumerable对象一</param>
        /// <param name="second">IEnumerable对象二</param>
        /// <returns>是否包含</returns>
        public static bool IsSubsetOf(this IEnumerable first, IEnumerable second)
        {
            var secondList = second.Cast<object>().ToList();
            foreach (var item in first)
            {
                var index = secondList.FindIndex(item.Equals);
                if (index < 0)
                {
                    return false;
                }
                secondList.RemoveAt(index);
            }
            return true;
        }

    }
}
